<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="input-group">
                <div class="custom-file">
                    <input id="txtFile" type="file" class="custom-file-input" accept="image/*">
                    <label class="custom-file-label" id="pr">选择图片 或 拖拽图片文件 或 Ctrl + V 粘贴图片</label>
                </div>
                <div class="input-group-prepend">
                    <button class="btn btn-info" title="逆时针旋转 5°" id="btnRotateLeft"><i class="fa fa-rotate-left"></i></button>
                </div>
                <div class="input-group-prepend">
                    <button class="btn btn-info" title="顺时针旋转 5°" id="btnRotateRight"><i class="fa fa-rotate-right"></i></button>
                </div>
                <div class="input-group-prepend">
                    <button class="btn btn-info" title="重置" id="btnReset"><i class="fa fa-refresh"></i></button>
                </div>
                <div class="input-group-prepend">
                    <button class="btn btn-warning" id="btnOcr">识别</button>
                </div>
            </div>
            <div class="mt-3">
                <div class="img-container">
                    <img id="imgview">
                </div>
            </div>
        </div>
        <div class="col-md-12">
            <textarea class="form-control d-none my-3 min300" id="txtResult" placeholder="这里显示识别结果"></textarea>
        </div>
    </div>
</div>

@Html.Raw(Netnr.SharedApp.QuoteTo.Html("cropperjs"))

<script>
    var pg = {
        init: function () {

            $('#btnRotateLeft').click(function () {
                if (pg.cp) {
                    pg.cp.rotate(-5)
                }
            });

            $('#btnRotateRight').click(function () {
                if (pg.cp) {
                    pg.cp.rotate(5)
                }
            });

            $('#btnReset').click(function () {
                if (pg.cp) {
                    pg.cp.reset()
                }
            });

            $('#btnOcr').click(function () {
                pg.scan();
            });

            //接收文件
            ss.receiveFiles(function (files) {
                var isImg = false;
                for (var i = 0; i < files.length; i++) {
                    if (files[i].type.indexOf("image") != -1) {
                        isImg = true;
                        pg.iv.src = URL.createObjectURL(files[i]);
                        pg.crop();
                        break;
                    }
                }
                if (!isImg) {
                    jz.alert('不是图片哦');
                }
            }, "#txtFile");
        },

        iv: document.getElementById('imgview'),

        crop: function () {
            if (pg.cp) {
                pg.cp.destroy();
            }
            pg.cp = new Cropper(pg.iv, { aspectRatio: NaN });
        },

        //将base64转换为blob
        dataURLtoBlob: function (dataurl) {
            var arr = dataurl.split(','),
                mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new Blob([u8arr], { type: mime });
        },
        //将blob转换为file
        blobToFile: function (theBlob, fileName) {
            theBlob.lastModifiedDate = new Date();
            theBlob.name = fileName;
            return theBlob;
        },

        //描述
        dodesc: function (msg) {
            $('#pr').html(msg == null ? '选择图片 或 拖拽图片文件 或 Ctrl + V 粘贴图片' : msg);
        },

        //扫码识别
        scan: function () {
            if (pg.cp) {
                pg.dodesc('正在上传...');

                var base64Data = pg.cp.getCroppedCanvas().toDataURL('image/jpeg');
                var blob = pg.dataURLtoBlob(base64Data);
                var file = pg.blobToFile(blob, "ocr.jpg");

                //上传
                var formData = new FormData();
                formData.append("file", file);

                $.ajax({
                    url: `${ss.apiServer}/aip/ocr`,
                    type: "POST",
                    data: formData,
                    contentType: false,
                    processData: false,
                    dataType: 'json',
                    xhr: function () {
                        xhr = $.ajaxSettings.xhr();
                        xhr.upload.addEventListener('progress', function (e) {
                            var rate = ((e.loaded / e.total) * 100).toFixed();
                            if (rate < 100) {
                                pg.dodesc('已上传 ' + rate + '%');
                            } else {
                                pg.dodesc('正在识别...');
                            }
                        })
                        return xhr;
                    },
                    success: function (data) {
                        console.log(data);
                        if (data.code == 200 && data.data.words_result) {
                            var wrs = [];
                            $.each(data.data.words_result, function () {
                                wrs.push(this.words);
                            });
                            $('#txtResult').removeClass('d-none').val(wrs.join('\r\n'));
                            document.documentElement.scrollTo(0, document.body.scrollHeight)
                        } else {
                            jz.alert('OCR接口异常')
                        }
                    },
                    error: function () {
                        jz.alert("上传出错");
                    },
                    complete: function () {
                        pg.dodesc();
                    }
                });
            }
        }
    }

    pg.init();
</script>